#ifndef _WIN32HELPER_H_
#define _WIN32HELPER_H_

#pragma once
#include <io.h>
#include <time.h>
#include <atltime.h>

#pragma comment(lib,"Version.lib")

namespace Win32Helper
{

class CDirectory
{
public:
	static void GetCurrentDirectory(__out ATL::CString& curDir)
	{
		TCHAR dir[MAX_PATH];
		::GetCurrentDirectory(sizeof(dir),dir);
		curDir = dir;
	}

	static bool IsDirectoryExist(__in LPCWSTR directoryPath)
	{
		return (_waccess(directoryPath,0)!=-1);
	}

	static BOOL CreateDirectory(__in LPCWSTR directoryPath)
	{
		return ::CreateDirectory(directoryPath,NULL);
	}
};

class CFile
{
public:
	static bool IsFileExist(__in LPCWSTR filePath)
	{
		return _waccess(filePath,0)!=-1;
	}

	static void DeleteFile(__in LPCWSTR filePath)
	{
		::DeleteFile(filePath);
	}
};

class CVersionInfo
{
public:
	WORD MajorVersion;  
	WORD MinorVersion;  
	WORD BuildNumber;  
	WORD RevisionNumber;

public:
	CVersionInfo()
		: MajorVersion(0)
		, MinorVersion(0)
		, BuildNumber(0)
		, RevisionNumber(0)
	{}

	BOOL GetVersionInfo(__in LPCTSTR filePath)
	{
		DWORD dwHandle, dwLen; 
		UINT BufLen; 
		LPTSTR lpData; 
		VS_FIXEDFILEINFO *pFileInfo; 
		dwLen = ::GetFileVersionInfoSize(filePath,&dwHandle); 
		if ( dwLen == 0 )
			return FALSE; 
		lpData = (LPTSTR) malloc (dwLen); 
		if (!lpData)   
			return FALSE; 
		if( !::GetFileVersionInfo(filePath, dwHandle, dwLen, lpData) ) 
		{  
			free (lpData);  
			return FALSE; 
		} 
		if( VerQueryValue( lpData, _T("\\"), (LPVOID *) &pFileInfo, (PUINT)&BufLen ) )  
		{  
			MajorVersion = HIWORD(pFileInfo->dwFileVersionMS);  
			MinorVersion = LOWORD(pFileInfo->dwFileVersionMS);  
			BuildNumber = HIWORD(pFileInfo->dwFileVersionLS);  
			RevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);  

			free (lpData);  
			return TRUE; 
		}
		free (lpData); 
		return FALSE;
	}

public:
	static ATL::CTime GetBuildDate() 
	{ 
		char s_month[5];
		int month, day, year;
		tm t = {0};
		static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

		char dateStr[] = __DATE__; 
		sscanf_s(dateStr, "%s %d %d", s_month, sizeof(s_month), &day, &year);

		month = (strstr(month_names, s_month)-month_names)/3;

		t.tm_mon = month;
		t.tm_mday = day;
		t.tm_year = year - 1900;
		t.tm_isdst = -1;

		time_t tt = mktime(&t);
		ATL::CTime ct(tt);
		return ct;
	}
};

} // namespace Win32Helper

#endif // _WIN32HELPER_H_